package com.neuronbit.businessflow.intellij.function;

import com.neuronbit.businessflow.intellij.editor.BpmHtmlFileEditor;
import com.intellij.ide.util.TreeClassChooser;
import com.intellij.ide.util.TreeClassChooserFactory;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.ui.jcef.JBCefJSQuery;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.cef.browser.CefBrowser;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.function.Function;

/**
 * The type Clazz filter function.
 */
public class ClazzFilterFunction implements Function<String, JBCefJSQuery.Response> {
    private final Project project;
    private final CefBrowser cefBrowser;
    private final String viewerUrl;

    /**
     * Instantiates a new Clazz filter function.
     *
     * @param project    the project
     * @param cefBrowser the cef browser
     * @param viewerUrl  the viewer url
     */
    public ClazzFilterFunction(Project project, @NotNull CefBrowser cefBrowser, String viewerUrl) {
        this.project = project;
        this.cefBrowser = cefBrowser;
        this.viewerUrl = viewerUrl;
    }

    @Override
    public JBCefJSQuery.Response apply(String params) { // 2
        Logger.getInstance(BpmHtmlFileEditor.class).info("message from browser " + params);


        EventQueue.invokeLater(() -> {
            TreeClassChooserFactory chooserFactory = TreeClassChooserFactory.getInstance(project);
            GlobalSearchScope searchScope = GlobalSearchScope.allScope(project);
            TreeClassChooser chooser =
                    chooserFactory.createNoInnerClassesScopeChooser(
                            "Select Class", searchScope, (psiClass) -> true, null);
            chooser.showDialog();
            final PsiClass selectedPsiClass = chooser.getSelected();
            if (null != selectedPsiClass) {
                String fieldId = null;
                if (StringUtils.isNotBlank(params)) {

                    try {
                        fieldId = JsonPath.read(params, "$.fieldId");
                    } catch (PathNotFoundException e) {
                        //ignore
                    }
                }
                cefBrowser.executeJavaScript( // 6
                        """
                                window.AliLowCodeEngine.event.emit('ClazzEvent', ['%s','%s']);
                                """.formatted(fieldId, selectedPsiClass.getQualifiedName()),
                        viewerUrl, 0
                );
            }
        });

        return null;
//
//        PsiClass psiClass = selectedPsiClass[0];
//        if (null != psiClass) {
//            return new JBCefJSQuery.Response(psiClass.getQualifiedName(), 0, "success"); // 3
//        }
//
//
//        return new JBCefJSQuery.Response("xyz", 0, "success"); // 3
    }
}
